﻿package com.zhangli.serivesoket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity     {

    private EditText edit;
    private TextView get_text;
    private Button button;

    private SocketServer server=new SocketServer ( 9999 );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);

        edit= (EditText) findViewById(R.id.edit);
        get_text= (TextView) findViewById(R.id.get_text);
        button= (Button) findViewById(R.id.button);

        /**socket服务端开始监听*/
        server.beginListen ( );

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**socket发送数据*/
                server.sendMessage ( edit.getText ().toString () );
            }
        });

        /**socket收到消息线程*/
        SocketServer.ServerHandler=new Handler(  ){
            @Override
            public void handleMessage(Message msg)
            {
                get_text.setText ( msg.obj.toString ());
            }
        };
    }
}
